﻿using System.Collections.Generic;
using System.Linq;
using RG.Consts;
using RealtorGuide.Classes;

namespace RG.Classes.Impl.Beta
{
    public class ApartmentFakeService : IApartmentService 
    {
        public Apartment Get(int id)
        {
            return ClassesContainer.Apartments.FirstOrDefault(x=>x.Id == id);
        }
        
        public Apartment Put(Apartment item)
        {
            if(item.Id!=0)
            {
                ClassesContainer.Apartments.Remove(ClassesContainer.Apartments.FirstOrDefault(x => x.Id == item.Id));
                ClassesContainer.Apartments.Add(item);
                return item;
            }

            item.Id = ++ClassesContainer.ApartmentGenerator;
            ClassesContainer.Apartments.Add(item);
            return item;
        }


        public List<Apartment> List(ApartmentsQueryParams param)
        {
            return ReturnFiltered(ClassesContainer.Apartments, param);
        }

 
        public List<int> ListPrepare(ApartmentsQueryParams param)
        {
            return ReturnFiltered(ClassesContainer.Apartments, param).Select(x=>x.Id).ToList();
        }


        private List<Apartment> ReturnFiltered(List<Apartment> source,ApartmentsQueryParams param)
        {
            var toReturn = new List<Apartment>();
            toReturn = source;

            if (!string.IsNullOrEmpty(param.Address))
                toReturn = toReturn.Where(x => x.Address.Contains(param.Address)).ToList();

            if (param.Areas != null && param.Areas.Any())
                toReturn = toReturn.Where(x => param.Areas.Contains(x.Area)).ToList();

            if (param.Cost != null)
                toReturn = toReturn.Where(x => x.Cost>=param.Cost.From && x.Cost<=param.Cost.To).ToList();

            if (param.Date != null)
                toReturn = toReturn.Where(x => x.CreateDate >= param.Date.minDate && x.CreateDate <= param.Date.maxDate).ToList();

            if (param.Floor != null)
                toReturn = toReturn.Where(x => x.Floor >= param.Floor.From && x.Floor <= param.Floor.To).ToList();

            if (param.Ids != null && param.Ids.Any())
                toReturn = toReturn.Where(x => param.Ids.Contains(x.Id)).ToList();

            if (!string.IsNullOrEmpty(param.ManagerName))
                toReturn = toReturn.Where(x => x.Manager.FIO.Contains(param.ManagerName)).ToList();

            if (param.Materials != null && param.Materials.Any())
                toReturn = toReturn.Where(x => param.Materials.Contains(x.TypeOfMaterial)).ToList();

            if (!string.IsNullOrEmpty(param.MicroAreaName))
                toReturn = toReturn.Where(x => x.MicroArea.Name.Contains(param.MicroAreaName)).ToList();

            if (param.NumberOfStoreys != null)
                toReturn = toReturn.Where(x => x.NumberOfStoreys >= param.NumberOfStoreys.From && x.NumberOfStoreys <= param.NumberOfStoreys.To).ToList();

            if (param.Rooms != null && param.Rooms.Any())
                toReturn = toReturn.Where(x => param.Rooms.Contains(x.CountOfRooms)).ToList();

            if (param.Square != null )
                toReturn = toReturn.Where(x => x.Square >= param.Square.From && x.Square <= param.Square.To).ToList();

            if (param.States != null && param.States.Any())
                toReturn = toReturn.Where(x => param.States.Contains(x.State)).ToList();
            
            return toReturn;
        }
        
    }
}
